package com.jixiang.tracedata.service;

import com.jixiang.tracedata.mapper.FlightMapper;
import com.jixiang.tracedata.mapper.TraceOriginalMapper;
import com.jixiang.tracedata.mapper.TravellerMapper;
import com.jixiang.tracedata.model.flight.*;
import com.jixiang.tracedata.util.StringOperate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author quanbiao
 * @date 2018-09-03
 */
@Service("flightService")
public class FlightService {

    @Autowired
    private FlightMapper flightMapper;

    @Autowired
    private TraceOriginalMapper traceOriginalMapper;


    @Transactional(rollbackFor = Exception.class)
    public void addTraceFlight(String xmlContent, Msg msg){
        //插入原始数据
        traceOriginalMapper.addTraceOriginalData(xmlContent);
        //插入航班数据
        addFlightMessage(msg);
        addFlightSegment(msg);
    }

    /**
     * 航班信息总的信息
     * @param msg
     * @return
     */
    public int addFlightMessage(Msg msg){
        if(null != msg.getDat() &&
                null != msg.getDat().getFlight() &&
                null != msg.getDat().getFlight().getLeg()){
            Leg leg = msg.getDat().getFlight().getLeg();
            List<CNFNum> cnfNumList = leg.getCNFNumList();
            if(null != cnfNumList && cnfNumList.size() != 0){
                String CNFNum = StringOperate.listObjectToString(cnfNumList);
                msg.setDB_Leg_CNFNum(CNFNum);
            }
            List<AVNum> avNumList = leg.getAVNumList();
            if(null != avNumList && avNumList.size() > 0){
                String AVNum = StringOperate.listObjectToString(avNumList);
                msg.setDB_Leg_AVNum(AVNum);
            }
            List<CAPNum> capNumList = leg.getCAPNumList();
            if(null != capNumList && capNumList.size() > 0){
                String CAPNum = StringOperate.listObjectToString(capNumList);
                msg.setDB_Leg_CAPNum(CAPNum);
            }
        }
        return flightMapper.addFlightMessage(msg);
    }

    /**
     * 航班信息 Segment 多条
     * @param msg
     * @return
     */
    public int addFlightSegment(Msg msg){
        //如果Segment节点有值的话 就插入Segment表中
        if(null != msg.getDat() && null != msg.getDat().getFlight()
                && null != msg.getDat().getFlight().getSegmentList()){
            List<Segment> segmentList = msg.getDat().getFlight().getSegmentList();
            for (Segment segment : segmentList) {
                //赋值xml头信息
                segment.setHdr(msg.getHdr());
                //Statistics
                Statistics statistics = segment.getStatistics();
                if(null != statistics){
                    List<BookNum> bookNumList = statistics.getBookNumList();
                    if(null !=  bookNumList && bookNumList.size() > 0){
                        String bookNum = StringOperate.listObjectToString(bookNumList);
                        segment.setDB_Segment_BookNum(bookNum);
                    }
                    List<CkiNum> ckiNumList = statistics.getCkiNumList();
                    if(null != ckiNumList && ckiNumList.size() > 0){
                        String ckiNum = StringOperate.listObjectToString(ckiNumList);
                        segment.setDB_Segment_CkiNum(ckiNum);
                    }
                    List<StandbyNum> standbyNumList = statistics.getStandbyNumList();
                    if(null != standbyNumList && standbyNumList.size() > 0){
                        String standbyNum = StringOperate.listObjectToString(standbyNumList);
                        segment.setDB_Segment_StandbyNum(standbyNum);
                    }
                }
                List<CodeShare> codeShareList = segment.getCodeShareList();
                if(null != codeShareList && codeShareList.size() > 0){
                    String codeShare = StringOperate.listObjectToString(codeShareList);
                    segment.setDB_CodeShare(codeShare);
                }

            }
            return flightMapper.addFlightSegment(segmentList);
        }else{
            return 0;
        }
    }

}
